/**
 * @author TannerMeans
 */
package com.btw;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

public class VSRenderer implements GLSurfaceView.Renderer{

	// member fields 
	private Group root;
	
	public VSRenderer(Group group) {
		this.root = group;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Redraw background color
		gl.glClearColor(0.5f, 0.0f, 1.0f, 1.0f);
		// allow the use of vertex arrays to draw shit!
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		//enable blending for transparency
		gl.glEnable(GL10.GL_BLEND);
	    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	    
		// set GL_MODELVIEW transformation mode
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity(); // reset matrix to its default state
	    
		gl.glOrthof(0f, 800f, 0f, 480f, -50f, 50f);
//		GLU.gluOrtho2D(gl, 0, 800, 0, 480);
	    //GLU.gluLookAt(gl, 0, 0, 100.0f, 0f, 0f, 0f, 0f, 1.0f, 0f);
	}
	
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);// OpenGL docs.
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
		// Reset the projection matrix
		gl.glLoadIdentity();// OpenGL docs.
		// Calculate the aspect ratio of the window
		
//		GLU.gluPerspective(gl, 45.0f,
//                                   (float) width / (float) height,
//                                   0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.
		// Reset the modelview matrix
		gl.glLoadIdentity();// OpenGL docs
//		GLU.gluOrtho2D(gl, 0, 800, 0, 480);
		gl.glOrthof(0f, 800f, 0f, 480f, -50f, 50f);
	}
	
	public void onDrawFrame(GL10 gl) {
		// Redraw background color
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		// set GL_MODELVIEW transformation mode
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity(); // reset matrix to its default state
		
		// set camera angle
		// 4th param, eye height, cannot be > 100.0f
//		GLU.gluLookAt(gl, 0, 0, 100.0f, 0f, 0f, 0f, 0f, 1.0f, 0f);
		gl.glOrthof(0f, 800f, 480f, 0f, -50f, 50f);
//		GLU.gluOrtho2D(gl, 0, 800, 0, 480);
		// draw all objects
		root.draw(gl);
	}
	
	public void addMesh(Mesh m) {
		root.add(m);
	}
}
